import java.awt.image.*;
import java.awt.*;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.event.*;


import java.awt.image.BufferStrategy;
	


public class Display extends JPanel
{
  private BufferStrategy strategy; 
  private Graphics2D buffer; 


  private Camera camera;
   
  private int middle;
   
  private Image grassTile;
  private Image grassTile2;

  private int[] point;
  private Point points;
  private Point pointe;
  private boolean []click;

  private BufferedImage mapImage;
  private Graphics2D mapGraph;

  private Point tmp = new Point(0,0);

  public Display(int width, int height,BufferStrategy bstrategy,Camera cam,int[] p,Point ps, Point pe,boolean []c)
    {
	super();
	
	click = c;
	points = ps;
	pointe = pe;
	camera = cam;
	point = p;
	middle = width / 2;
	//setVisible(true);
	strategy = bstrategy;
	buffer = (Graphics2D) bstrategy.getDrawGraphics();
	
	
	initImages();
		
	
    }

    public void moveCamera(double vx, double vy)
    {
	camera.move(vx,vy);
    }

    public Camera getCamera(){
	return camera;
    }

    private void initImages() //charge les images 
  {
      try{
	while (grassTile == null ){
	  
	    
	  grassTile  = this.getToolkit().getImage("grassTile_1.gif");//ImageIO.read(new File("grassTile_1.png"));
	  
	}}catch(Exception e){;}
      
      prepareImage(grassTile, this);
      

      /* mapImage = new BufferedImage(1000,600,BufferedImage.TYPE_4BYTE_ABGR); 
      mapGraph = (Graphics2D)mapImage.createGraphics();

      

      mapGraph.setColor(Color.black);
      mapGraph.fillRect(0,0,(int)camera.getWidth(),(int)camera.getHeight());
	
      
      mapGraph.setColor(Color.red);
      for (int i = 0; i < 400; i+=40){
	for (int j = 0; j < 400; j+=40)
	  {
	    
	    
	    mapGraph.drawImage(grassTile,middle + i-j +(int)camera.getX() ,100+ (i+j +(int)camera.getY() )/2 ,null);
	    mapGraph.fillRect(middle + i-j +(int)camera.getX() ,100+ (i+j +(int)camera.getY() )/2 ,
			    1,1);
	    mapGraph.fillRect(middle + i-j +(int)camera.getX() + 80 ,100+ (i+j +(int)camera.getY() )/2 ,
			    1,1);
	  }
	  }*/
    }
      
   
    
    /* public void update(Graphics g)
    {
	
	paint(g);
	
    }

    public void paint(Graphics g)*/

    public void display(){
	buffer.setColor(Color.black);
	buffer.fillRect(0,0,(int)camera.getWidth(),(int)camera.getHeight());
	

	buffer.setColor(Color.red);
	for (int i = 0; i < 18; i+=40){
	    for (int j = 0; j < 18; j+=40)
		{
		    
		    
		  buffer.drawImage(grassTile,middle + i-j +(int)camera.getX() ,100+ (i+j +(int)camera.getY() )/2 ,null);
		  
		}



	}

	
	/*
	buffer.setColor(Color.blue);
	buffer.fillRect(middle + 40 + point[0] - point[1] + (int) camera.getX(),
			100 + (point[0] + point[1] + (int)camera.getY())/2,
			3,3);

	buffer.fillRect(middle + 40 + (200-200) + (int) camera.getX(),
			100 +( 200+200 + (int)camera.getY())/2,
			3,3);
	
	*/
	if (click[0])
	  {
	    BufferedImage bi = new BufferedImage(1000,600,BufferedImage.TYPE_4BYTE_ABGR);
	    Graphics2D g2d = (Graphics2D)bi.createGraphics();
	    
	    float alpha = 0.1f;
	    g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
	    g2d.setColor(Color.red);
	    
	    tmp.move((int)((points.getX() < pointe.getX())? points.getX() : pointe.getX()),
		     (int)((points.getY() < pointe.getY())? points.getY() : pointe.getY()));

	    g2d.fillRect((int)tmp.getX(),
			 (int)tmp.getY(),
			 Math.abs((int)(pointe.getX()-points.getX())),
			 Math.abs((int)(pointe.getY()-points.getY())));
	   
	    buffer.drawImage(bi,0,0,null);
	  }

    

	// buffer.drawImage(mapImage,(int)camera.getX() ,(int)camera.getY(),null);
      strategy.show();
    }
}